import { Module } from 'vuex'
import { ITopSongState } from './type'
import { IRootState } from '../types'

import {
  getTopSong,
  getTopList,
  getNewMv,
  getMvUrl,
  getNewVideo,
  getVideoType,
  getVideoUrl,
  getVideoTypeList
} from '@/service/music/music'
const topSongModule: Module<ITopSongState, IRootState> = {
  namespaced: true,
  state() {
    return {
      topToplist: '',
      topList: '',
      mvList: '',
      mvUrl: '',
      videoUrl: '',
      videoList: '',
      videoTypeList: ''
    }
  },
  mutations: {
    changeTopSong(state, changeTopSong) {
      state.topToplist = changeTopSong
    },
    changeTopList(state, topList) {
      state.topList = topList
    },
    changeMvList(state, mvList) {
      state.mvList = mvList
    },
    changeMvURL(state, mvUrl) {
      state.mvUrl = mvUrl
    },
    changeVideoList(state, videoList) {
      state.videoList = videoList
    },
    changeVideoURL(state, videoUrl) {
      state.videoUrl = videoUrl
    },
    changevideoType(state, videoTypeList) {
      state.videoTypeList = videoTypeList
    }
  },
  actions: {
    async getTopSong({ commit }, type: number) {
      const { data: infoRes } = await getTopSong(type)
      const songInfo: any[] = []
      infoRes.map(
        (item: {
          name: any
          artists: { name: any }[]
          id: any
          album: { artists: any[]; publishTime: any; picUrl: any }
          mv: any
        }) => {
          return songInfo.push({
            songName: item.name,
            singer: item.artists[0].name,
            singID: item.id,
            singAl: item.album.artists[0],
            time: item.album.publishTime,
            songImg: item.album.picUrl,
            mv: item.mv
          })
        }
      )
      const randomS = Math.floor(Math.random() * 80)
      const randomE = Number(randomS + 8)

      commit('changeTopSong', songInfo.slice(randomS, randomE))
      // commit('changeSongListInfo', infoRes)
    },
    async getTopList({ commit }) {
      const { list: topList } = await getTopList()
      const randomS = Math.floor(Math.random() * 15)
      const randomE = Number(randomS + 12)
      commit('changeTopList', topList.slice(randomS, randomE))
    },
    async getNewMv({ commit }) {
      const { data: mvList } = await getNewMv()

      const randomS = Math.floor(Math.random() * 15)
      const randomE = Number(randomS + 12)
      commit('changeMvList', mvList.slice(randomS, randomE))
    },
    async getMvUrl({ commit }, mvID) {
      const { data } = await getMvUrl(mvID)

      commit('changeMvURL', data.url)
    },
    async getNewVideo({ commit }) {
      const { datas: data } = await getNewVideo()
      const videoList: any[] = []
      data.map((item: { data: any }) => {
        return videoList.push(item.data)
      })
      console.log()

      commit('changeVideoList', videoList)
    },
    async getVideoUrl({ commit }, mvID) {
      const { urls: videoUrl } = await getVideoUrl(mvID)

      commit('changeVideoURL', videoUrl[0].url)
    },
    async getVideoType({ commit }) {
      const { data: videoTypeList } = await getVideoType()

      commit('changevideoType', videoTypeList)
    },
    async getVideoTypeList({ commit }, videoType) {
      const { datas: data } = await getVideoTypeList(videoType)
      const videoList: any[] = []
      data.map((item: { data: any }) => {
        return videoList.push(item.data)
      })

      commit('changeVideoList', videoList)
    }
  },
  getters: {}
}
export default topSongModule
